<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row>
          <el-col :md="4" :sm="12">
            <el-form-item label="起始日期:">
              <el-date-picker size="mini" v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="4" :sm="12">
            <el-form-item label="结束日期:">
              <el-date-picker size="mini" v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="药品:" label-width="50px">
              <el-input size="mini"
                        clearable
                        v-model="where.materialcode"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="批号:" label-width="50px">
              <el-input size="mini"
                        clearable
                        v-model="where.batch"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="3" :md="12">
            <el-form-item label="状态:" label-width="50px">
              <el-select size="mini"
                         clearable
                         v-model="where.status"
                         placeholder="请选择"
                         class="ele-fluid">
                <el-option label="待处理" value="待处理"></el-option>
                <el-option label="已处理" value="已处理"></el-option>
                <el-option label="已反馈" value="已反馈"></el-option>
                <el-option label="已执行" value="已执行"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions" style="padding-top: 4px">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="mini"
                @click="reload">查询
              </el-button>
              <el-button
                type="warning"
                icon="el-icon-download"
                class="ele-btn-icon"
                size="mini"
                v-if="permission.includes('sys:qfffeedback:sync')"
                @click="syncFeedback">获取
              </el-button>
              <el-button @click="reset" size="mini" class="ele-btn-icon">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :toolbar="false"
        :selection.sync="selection"
        :page-size="20"
        :header-cell-style="{'text-align':'center'}"
        highlight-current-row
        @row-click="loadAttach"
        height="calc(100vh - 390px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:kdlotherinoutb:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:kdlotherinoutb:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            :disabled="row.status=='已反馈'"
            @click="openEdit(row)"
            v-if="permission.includes('sys:qfffeedback:save')">处理反馈
          </el-link>
          <el-link
            type="primary"
            :disabled="row.status=='已反馈' || row.status=='待处理'"
            :underline="false"
            icon="el-icon-upload"
            @click="submitFeedback(row)"
            v-if="permission.includes('sys:qfffeedback:submit')">反馈下发
          </el-link>
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志</el-link>
        </template>
      </ele-pro-table>

      <el-tabs v-model="activeName" @tab-click="handleClick" type="card">
        <el-tab-pane label="异常反馈附件"  name="first">
          <ele-pro-table
            ref="tableAttach"
            :datasource="attachList"
            :columns="columnsAttach"
            :toolbar="false"
            :need-page="false"
            :page-size="20"
            :header-cell-style="{'text-align':'center'}"
            style="width: 55%"
            height="170px">
            <!-- 表头工具栏 -->
            <template slot="toolbar" v-if="false">
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                class="ele-btn-icon"
                @click="openEdit(null)"
                v-if="permission.includes('sys:kdlotherinoutb:add')">添加
              </el-button>
              <el-button
                size="small"
                type="danger"
                icon="el-icon-delete"
                class="ele-btn-icon"
                @click="removeBatch"
                v-if="permission.includes('sys:kdlotherinoutb:dall')">删除
              </el-button>
            </template>
            <!-- 操作列 -->
            <template slot="action" slot-scope="{row}">
              <el-link
                type="primary"
                :underline="false"
                icon="el-icon-preview"
                @click="openAttach(row)"
              >预览
              </el-link>
            </template>
          </ele-pro-table>
        </el-tab-pane>
        <el-tab-pane label="移库结果" name="second">
          <ele-pro-table
            ref="tableAttach"
            :datasource="gmList"
            :columns="columnsGM"
            :toolbar="false"
            :need-page="false"
            :page-size="200"
            :header-cell-style="{'text-align':'center'}"
            style="width: 100%"
            height="170px">
            <!-- 表头工具栏 -->
            <template slot="toolbar" v-if="false">
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                class="ele-btn-icon"
                @click="openEdit(null)"
                v-if="permission.includes('sys:kdlotherinoutb:add')">添加
              </el-button>
              <el-button
                size="small"
                type="danger"
                icon="el-icon-delete"
                class="ele-btn-icon"
                @click="removeBatch"
                v-if="permission.includes('sys:kdlotherinoutb:dall')">删除
              </el-button>
            </template>
            <!-- 操作列 -->
            <template slot="action" slot-scope="{row}">
              <el-link
                type="primary"
                :underline="false"
                icon="el-icon-preview"
                @click="openAttach(row)"
              >预览
              </el-link>
            </template>
          </ele-pro-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 编辑弹窗 -->
        <FeedbackEdit
          :data="current"
          :visible.sync="showEdit"
          @done="reload"/>
    <AttachPreview
      :data="currentAttach"
      :attachpath="'qff'"
      :visible.sync="showAttach"/>
    <LogList
      :dataItem="current"
      :visible.sync="showLog"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import LogList from '../../../purchase/inbound/loglist';
import FeedbackEdit from './feedback-edit'
import AttachPreview from './attach-preview'
import setting from "@/config/setting";
export default {
  name: 'ExamKdlQffFeedback',
  components: {LogList,FeedbackEdit,AttachPreview},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/kdl/qfffeedbacklist',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        // },
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
        },
        {
          prop: 'erdat',
          label: '单据日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'qffno',
          label: 'QFF编号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'orderno',
          label: '来源单号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'itemno',
          label: '药品编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'comdescription',
          label: '通用名',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'materialspec',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'manufacturername',
          label: '生产企业',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'qty',
          label: '异常数量',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'materialunit',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'result',
          label: '异常说明',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'left'
        },
        {
          prop: 'vendorbatch',
          label: '批号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'manufacturedate',
          label: '生产日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'expireddate',
          label: '有效期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'registrationfilingapplicantname',
          label: '上市许可持有人',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'materialtype',
          label: '药品类别',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'left'
        },
        // {
        //   prop: 'location',
        //   label: '库位',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        {
          prop: 'frombindesc',
          label: '库位名称',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'left'
        },

        {
          prop: 'plant',
          label: '工厂',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        // {
        //   prop: 'erdat',
        //   label: 'QFF发起日期',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        {
          prop: 'tobindesc',
          label: '处理库位',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        // {
        //   prop: 'tobindesc',
        //   label: '名称',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'left'
        // },
        {
          prop: 'feedback',
          label: '处理意见',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'operUser',
          label: '处理人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'operTime',
          label: '处理日期',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'principalorderno',
          label: '反馈指令号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'inspectiontype',
          label: '检验单类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 240,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      columnsAttach:[
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
        },
        {
          prop: 'attachfile',
          label: '附件',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 100,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }

      ],
      columnsGM: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'orderdate',
          label: '单据日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'docno',
          label: 'SAP单号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'orgname',
          label: '事业部',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'itemno',
          label: '药品编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'comdescription',
          label: '通用名',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'materialspec',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'materialunit',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'qty',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'vendorbatch',
          label: '批号',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'fromlocation',
          label: '转出库位',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'frombindesc',
          label: '库位名称',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'tolocation',
          label: '转入库位',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'tobindesc',
          label: '库位名称',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'boxflag',
          label: '箱型',
          showOverflowTooltip: true,
          minWidth: 70,
          align: 'center'
        },
        {
          prop: 'boxno',
          label: '箱号',
          showOverflowTooltip: true,
          minWidth: 220,
          align: 'center'
        },
        {
          prop: 'materialtype',
          label: '药品类型',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'lregistno',
          label: '批准文号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'left'
        },
        {
          prop: 'materialform',
          label: '剂型',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left',
        },
        {
          prop: 'manufacturername',
          label: '生产企业',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'batchno',
          label: '批次',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'billsource',
          label: '来源',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'plant',
          label: '工厂',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'gmname',
          label: '移库人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        // {
        //   prop: 'orderno',
        //   label: 'SAP物料凭证号',
        //   showOverflowTooltip: true,
        //   minWidth: 120,
        //   align: 'center'
        // },
        {
          prop: 'principalorderno',
          label: '信谊指令号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'doctype',
          label: '单据类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'ordertypedesc',
          label: '业务类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        // {
        //   prop: 'sn',
        //   label: 'SN',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      activeName:'first',
      // 表格搜索条件
      where: {transtype:'trans',riqiBegin:setting.getFromDate(),riqiEnd:new Date()},
      // 表格选中数据
      selection: [],
      attachList: [],
      gmList:[],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog:false,
      currentAttach: null,
      showAttach:false,
    };
  },
  methods: {
    handleClick(){

    },
    async loadGmDetail(row){
      var res = await this.$http.get('/kdlgm/detail?principalorderno='+row.principalorderno);
      if(res.status==200)
      {
        this.gmList = res.data.data;
      }
    },

    loadAttach(row){
      const loading = this.$loading({lock: true});
      this.loadGmDetail(row);
      this.$http.get('/kdl/feedbackattach?qffno='+row.qffno, {}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.attachList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {transtype:'trans',riqiBegin:setting.getFromDate(),riqiEnd:new Date()};
      this.reload();
    },
    syncFeedback(){
      const loading = this.$loading({lock: true});
      this.$http.post('/kdl/syncqfffeedback', {}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });

    },
    openLog(row){
      this.current = row;
      this.current.chukNo = row.qffno;
      //console.log(this.current);
      this.showLog = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示附件预览窗口 */
    openAttach(row) {
      this.currentAttach = row;
      this.showAttach = true;
    },
    /* 异常处理意见反馈至康德乐 */
    submitFeedback(row) {
      this.$confirm('确定要将选中的处理意见下发至KDL吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/kdl/uploadfeedback/'+ row.id).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
      // this.current = row;
      // this.showEdit = true;
    },    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/kdlotherinoutb/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的其他出入库明细吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/kdlotherinoutb/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.ele-form-search{
  margin-top: -15px ;
  margin-bottom: -5px;
}
.el-form-item {
  margin-bottom: -5px !important;
}
::v-deep .el-tabs__item {
  padding: 0 20px;
  height: 30px;
  box-sizing: border-box;
  line-height: 30px;
  display: inline-block;
  list-style: none;
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text-primary);
  position: relative;
}
</style>
